﻿using System;
using System.Threading.Tasks;

using ApproveFlowEngine.Localization;
using ApproveFlowEngine.MultiTenancy;

using Microsoft.Extensions.Configuration;

using Volo.Abp.Account.Localization;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.UI.Navigation;

namespace ApproveFlowEngine.Menus
{
    public class ApproveFlowEngineMenuContributor : IMenuContributor
    {
        private readonly IConfiguration _configuration;

        public ApproveFlowEngineMenuContributor(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
            else if (context.Menu.Name == StandardMenus.User)
            {
                await ConfigureUserMenuAsync(context);
            }
        }

        private Task ConfigureUserMenuAsync(MenuConfigurationContext context)
        {
            var l = context.GetLocalizer<ApproveFlowEngineResource>();
            var accountStringLocalizer = context.GetLocalizer<AccountResource>();
            var authServerUrl = _configuration["AuthServer:Authority"] ?? "";

            context.Menu.AddItem(new ApplicationMenuItem("Account.Manage", accountStringLocalizer["MyAccount"],
                    $"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", icon: "fa fa-cog", order: 1000, null, "_blank").RequireAuthenticated());
            context.Menu.AddItem(new ApplicationMenuItem("Account.Logout", l["Logout"], url: "~/Account/Logout", icon: "fa fa-power-off", order: int.MaxValue - 1000).RequireAuthenticated());

            return Task.CompletedTask;
        }

        private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            var administration = context.Menu.GetAdministration();
            var l = context.GetLocalizer<ApproveFlowEngineResource>();

            context.Menu.Items.Insert(
                0,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.Home",
                    l["Menu:Home"],
                    "/",
                    icon: "fas fa-home",
                    order: 0
                )

            );

            context.Menu.Items.Insert(
                1,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.Account",
                    l["Menu:Account"],
                    "/Account2",
                    icon: "fas fa-home",
                    order: 0
                )

            );
            ///identity/roles
            context.Menu.Items.Insert(
            2,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.identity.roles",
                    l["Menu:identity:roles"],
                    "/identity/roles",
                    icon: "fas fa-home",
                    order: 0
                )

            );
            context.Menu.Items.Insert(
            3,
                new ApplicationMenuItem(
                    "ApproveFlowEngineMenus.identity.users",
                    l["Menu:identity:users"],
                    "/identity/users",
                    icon: "fas fa-home",
                    order: 0
                )

            );
           

            if (MultiTenancyConsts.IsEnabled)
            {
                administration.SetSubItemOrder("GroupName", 1);
            }
            else
            {
                administration.TryRemoveMenuItem("GroupName");
            }

            administration.SetSubItemOrder("Identity", 2);
            administration.SetSubItemOrder("Setting", 3);

            return Task.CompletedTask;
        }
    }
}
